package Midium;

//蛇梯棋
public class Solution909 {
    public static int snakesAndLadders(int[][] oldBoard) {
        int edgeLength = oldBoard.length;
        int end = edgeLength * edgeLength;
        int start = 1;
        int step = 0;
        int[][] board = new int[edgeLength][edgeLength];
        for (int i = 0; i < edgeLength; i++) {
            for (int j = 0; j < edgeLength; j++) {
                board[i][j] = oldBoard[edgeLength - i - 1][j];
            }
        }
        while (start < end) {
            if (start + 6 >= end)
                return step + 1;
            int newStart[] = new int[6];
            for (int i = 1; i < 7; i++) {
                //先把自己的编号转化为r行c列
                int remainder = (start + i) % edgeLength;
                int row = (start + i) / edgeLength;
                if(remainder==0)
                    row--;
                if(row>=edgeLength)
                    return step+1;
                int col = -1;
                //判断row是奇数还是偶数，奇数行从左往右，偶数从右往左
                if (row % 2 == 0) {
                    col = remainder == 0 ? edgeLength - 1 : remainder - 1;
                } else {
                    col = remainder == 0 ? 0 : edgeLength - remainder;
                }
                if (board[row][col] == -1) {
                    newStart[i - 1] = start + i;
                } else {
                    newStart[i - 1] = board[row][col];
                }
                if(newStart[i-1]>=end)
                    return step+1;
            }
            //检查，如果newStart里所有值都一样，则说明卡在死循环，直接返回-1
            boolean check = false;
            for(int k = 1 ; k < newStart.length ; k++){
                if(newStart[0]!=newStart[k])
                    check = true;
            }
            if(!check)
                return -1;
            //基于贪心，再判断一下newStart为起点的newNewStart
            int newNewStart[][] = new int[6][6];
            for (int i = 1; i < 7; i++) {
                for (int j = 1; j < 7; j++) {
                    int remainder = (newStart[i - 1] + j) % edgeLength;
                    int row = (newStart[i - 1] + j) / edgeLength;
                    if(remainder==0)
                        row--;
                    int col = -1;
                    if (row % 2 == 0) {
                        col = remainder == 0 ? edgeLength - 1 : remainder - 1;
                    } else {
                        col = remainder == 0 ? 0 : edgeLength - remainder;
                    }
                    //row溢出
                    if(row>=edgeLength){
                        newNewStart[i-1][j-1] = -1;
                        continue;
                    }
                    try {
                        if (board[row][col] == -1) {
                            newNewStart[i - 1][j - 1] = newStart[i - 1] + j;
                        } else {
                            newNewStart[i - 1][j - 1] = board[row][col];
                        }
                    }catch (Exception e){
                        System.out.println("----出现了异常----");
                        System.out.println("异常内容为：");
                        System.out.println("row="+row+",col="+col);
                        System.out.println("newNewStart=");
                        for(int ii = 0 ; ii < 6 ; ii++){
                            for(int jj = 0 ; jj < 6 ; jj++){
                                System.out.print(newNewStart[ii][jj]+",");
                            }
                            System.out.println("----");
                        }
                        System.out.println("newStart=");
                        for(int ii = 0 ; ii<newStart.length ; ii++){
                            System.out.print(newStart[ii]+",");
                        }
                        System.out.println();
                        System.out.println("出现异常的原因在于矩阵row溢出");
                    }
                }
            }
            //看一下newNewStart里哪个最大
            int max = -1;
            int maxStart = 0;
            for (int i = 0; i < newNewStart.length; i++) {
                for (int j = 0; j < 6; j++) {
                    if (newNewStart[i][j] > maxStart) {
                        maxStart = newNewStart[i][j];
                        max = i + 1; //代表着start刚开始掷骰子选增加max个格子最好
                    }
                }
            }
            start = start + max;
            int remainder = start % edgeLength;
            int row = start / edgeLength;
            int col;
            //判断row是奇数还是偶数，奇数行从左往右，偶数从右往左
            if (row % 2 == 0) {
                col = remainder == 0 ? edgeLength - 1 : remainder - 1;
            } else {
                col = remainder == 0 ? 0 : edgeLength - remainder;
            }
            if (board[row][col] != -1)
                start = board[row][col];
            step++;
        }
        return step;
    }

    public static void main(String[] args) {
//        int[][] arr = {{-1, -1, -1, -1, -1, -1},
//                {-1, -1, -1, -1, -1, -1},
//                {-1, -1, -1, -1, -1, -1},
//                {-1, 35, -1, -1, 13, -1},
//                {-1, -1, -1, -1, -1, -1},
//                {-1, 15, -1, -1, -1, -1}};
        int[][] arr = {
                {1,1,-1},
                {1,1,1},
                {-1,1,1}
        };
        System.out.println(snakesAndLadders(arr));
    }
}
